<template>
  <div class="">
    <div class="container">
      <el-tabs v-model="message">
        <el-tab-pane :label="`未读消息(${unread.length})`" name="first">
          <el-table :data="unread" :show-header="false" style="width: 100%">
            <el-table-column>
              <template #default="scope">
                <span class="message-title">{{ scope.row.title }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="date" width="180" />
            <el-table-column width="120">
              <template #default="scope">
                <el-button size="small" @click="handleRead(scope.$index)">
                  标为已读
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="handle-row">
            <el-button type="primary" @click="msgeStore.handleRead()"> 全部标为已读 </el-button>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`已读消息(${read.length})`" name="second">
          <template v-if="message === 'second'">
            <el-table :data="read" :show-header="false" style="width: 100%">
              <el-table-column>
                <template #default="scope">
                  <span class="message-title">{{ scope.row.title }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="date" width="150" />
              <el-table-column width="120">
                <template #default="scope">
                  <el-button type="danger" @click="handleDel(scope.$index)">
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="handle-row">
              <el-button type="danger" @click="msgeStore.handleDel()" > 删除全部 </el-button>
            </div>
          </template>
        </el-tab-pane>
        <el-tab-pane :label="`回收站(${recycle.length})`" name="third">
          <template v-if="message === 'third'">
            <el-table :data="recycle" :show-header="false" style="width: 100%">
              <el-table-column>
                <template #default="scope">
                  <span class="message-title">{{ scope.row.title }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="date" width="150" />
              <el-table-column width="120">
                <template #default="scope">
                  <el-button @click="handleRestore(scope.$index)">
                    还原
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="handle-row">
              <el-button type="danger" @click="msgeStore.handleRestore()"> 清空回收站 </el-button>
            </div>
          </template>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";
import {useMessageStore} from '@/store/message.js'
import {storeToRefs} from 'pinia'
const msgeStore = useMessageStore();
const { unread, read, recycle } = storeToRefs(msgeStore);

const message = ref("first");
const showHeader = ref(false);


const unreadNum = computed(() => {
  return unread.value.length;
});
const clearAll = () => {
}

const handleRead = (index) => {
  msgeStore.handleRead(index)
};
const handleDel = (index) => {
 msgeStore.handleDel(index)
};
const handleRestore = (index) => {
 msgeStore.handleRestore(index)
};
</script>

<style lang="scss" scoped>
.message-title {
  cursor: pointer;
}
.handle-row {
  margin-top: 30px;
}
</style>

